<template>
  <div>
    <!-- 中间省略 -->
    <template v-if="type === 'center'">
      <div class="sz-center-ellipsis">
        <div v-getDom="handleLoad" class="all-text" v-if="!isOverflow">
          {{ content }}
        </div>
        <el-popover
          :content="content"
          :width="tipWidth"
          :trigger="tipTrigger"
          :disabled="!tooltip"
        >
          <div slot="reference">
            <div class="ellipsis-box" v-if="isOverflow">
              <div class="front">{{ content }}</div>
              <div class="end">{{ content.slice(suffixLength * -1) }}</div>
            </div>
          </div>
        </el-popover>
      </div>
    </template>
    <!-- 末尾省略 -->
    <template v-if="type === 'end'">
      <div class="sz-end-ellipsis">
        <el-popover
          :content="content"
          :width="tipWidth"
          :trigger="tipTrigger"
          :disabled="!tooltip || !isOverflow"
        >
          <div slot="reference">
            <div
              v-getDom="handleLoad"
              class="all-text"
              :style="{ '-webkit-line-clamp': lineClamp }"
            >
              {{ content }}
            </div>
          </div>
        </el-popover>
      </div>
    </template>
    <!-- 展开收起省略 -->
    <template v-if="type === 'fold'">
      <FoldEllipsis
        :lines="lineClamp"
        :fontSize="fontSize"
        :lineHeight="lineHeight"
      >
        {{ content }}
      </FoldEllipsis>
    </template>
  </div>
</template>

<script>
import FoldEllipsis from "./fold.vue";
export default {
  name: "SzEllipsis",
  components: { FoldEllipsis },
  props: {
    // 省略方式
    type: {
      type: String,
      default: "end",
      validator: (value) => {
        return ["center", "end", "fold"].includes(value);
      }
    },
    // 中间省略文本时，后面保留几个字
    suffixLength: {
      type: Number,
      default: 4
    },
    // 超过几行省略（type取end、fold生效）
    lineClamp: {
      type: Number,
      default: 1
    },
    content: {
      type: String,
      default: "",
      required: true
    },
    // 省略时是否展示tooltip，默认展示
    tooltip: {
      type: Boolean,
      default: true
    },
    tipWidth: {
      type: Number,
      default: 300
    },
    tipTrigger: {
      type: String,
      default: "hover",
      validator: (value) => {
        return ["click", "focus", "hover", "manual"].includes(value);
      }
    },
    fontSize: {
      type: Number,
      default: 14
    },
    lineHeight: {
      type: Number,
      default: 20
    }
  },
  data() {
    return {
      isOverflow: false,
      isExpand: false
    };
  },
  methods: {
    handleLoad(e) {
      if (e.clientWidth < e.scrollWidth) {
        this.isOverflow = true;
      }
      // 多行省略时，判断高度
      if (e.clientHeight < e.scrollHeight) {
        this.isOverflow = true;
      }
    },
    toggle() {
      this.isExpand = !this.isExpand;
    }
  }
};
</script>

<style lang="scss" scoped>
.sz-center-ellipsis {
  .all-text {
    width: 100%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .ellipsis-box {
    display: flex;
    .front {
      flex: 1;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .end {
      white-space: nowrap;
    }
  }
}
.sz-end-ellipsis {
  .all-text {
    width: 100%;
    word-break: break-all;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
  }
}
.sz-fold-ellipsis {
  .all-text {
    width: 100%;
    word-break: break-all;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
  }
  .btn {
    float: right;
    clear: both;
    font-size: 14px;
    line-height: 20px;
  }
  .btn-text {
    color: #fd3e0a;
    cursor: pointer;
    margin-left: 4px;
    font-size: 14px;
    line-height: 20px;
  }
}
</style>
